﻿#define NOMINMAX

#include <chrono>
#include <fstream>
#include <iostream>
#include <thread>

#include "../src/util/time_util.hh"
#include "box/service_box.hh"
#include "box/service_context.hh"
#include "detail/lang_impl.hh"

void setup() {
  {
    std::ofstream ofs;
    ofs.open("config_service_box1.cfg", std::ios::trunc | std::ios::out);
    ofs << "listener = {\r"
           "   host = (\"127.0.0.1:20000\")\r"
           "}\r"

           "box_channel_recv_buffer_len = 1024\r"
           "box_name = \"guild_service\"\r"
           "logger_config_line = "
          //  "\"file:\/\/.;file=box_%y%m%d.log;line=[%y%m%d-%H:%M:%S:%U];flush="
          //  "true;mode=day;maxSize=10240000\"\r"
           "\"console://.;line=[%y%m%d-%H:%M:%S:%U];flush=true\"\r"

           "service = {\r"
           "   service_dir = "
#ifdef _WIN32
           "\"../../../../../../../publish/bin/service/Debug\"\r"
#else
           "\"../../../../../../../publish/bin/service\"\r"
#endif // _WIN32
           "   preload_service = "
           "<2903585527315105956:\"libGuildServiceBasic.so\","
           "8489522935280762878:\"libGuildServiceWelfare.so\","
           "269471807845479163:\"libGuildServiceTimer.so\","
           "5985686777450930310:\"libGuildServiceAuth.so\">\r"
           "}\r"

           "debugger = {\r"
           "   enable = \"true\"\r"
           "   listen = \"0.0.0.0:10888\"\r"
           "}\r"

           "open_coroutine = \"true\"\r"

           "service_finder = {\r"
           "   type = \"zookeeper\"\r"
           "   hosts = \"127.0.0.1:2181\"\r"
           "}\r"

           "statistics = {\r"
           "   is_open_rpc_stat = \"true\"\r"
           "}\r"

           "redis = {\r"
           "   host = \"127.0.0.1\"\r"
           "   port = 6379\r"
           "   user = \"\"\r"
           "   passwd = \"\"\r"
           "}\r";
    ofs.close();
  }
  {
    std::ofstream ofs;
    ofs.open("config_service_box2.cfg", std::ios::trunc | std::ios::out);
    ofs << "listener = {\r"
           "   host = (\"127.0.0.1:10001\")\r"
           "}\r"

           "box_channel_recv_buffer_len = 1024\r"
           "box_name = \"guild_service\"\r"
           "logger_config_line = "
          //  "\"file:\/\/.;file=box_%y%m%d.log;line=[%y%m%d-%H:%M:%S:%U];flush="
          //  "true;mode=day;maxSize=10240000\"\r"
           "\"console://.;line=[%y%m%d-%H:%M:%S:%U];flush=true\"\r"

           "service = {\r"
           "   service_dir = "
#ifdef _WIN32
           "\"../../../../../../../publish/bin/service/Debug\"\r"
#else
           "\"../../../../../../../publish/bin/service\"\r"
#endif // _WIN32
           "   preload_service = "
           "<7805157159507665330:\"libGuildServiceQuery.so\","
           "8245634006695610382:\"libGuildServiceRank.so\","
           "815421464299229037:\"libGuildServiceActivity.so\","
           "1139088915790950834:\"libGuildServiceBuilding.so\","
           "5390625986414749139:\"libGuildServiceTask.so\","
           "4367918505680980634:\"libGuildServiceChat.so\">"
           "}\r"

           "debugger = {\r"
           "   enable = \"true\"\r"
           "   listen = \"0.0.0.0:10888\"\r"
           "}\r"

           "open_coroutine = \"true\"\r"

           "service_finder = {\r"
           "   type = \"zookeeper\"\r"
           "   hosts = \"127.0.0.1:2181\"\r"
           "}\r"

           "statistics = {\r"
           "   is_open_rpc_stat = \"true\"\r"
           "}\r"

           "redis = {\r"
           "   host = \"127.0.0.1\"\r"
           "   port = 6379\r"
           "   user = \"\"\r"
           "   passwd = \"\"\r"
           "}\r";
    ofs.close();
  }
}

void run(int argc, const char **argv) {
  kratos::service::ServiceBox service;

  if (argc > 1) {
    if (!service.start(argc, argv)) {
      return;
    }
  }
  else {
    int argc_default = 2;
    const char *argv_default[] = {"guild_service",
                                "--config=config_service_box1.cfg"};
    if (!service.start(argc_default, argv_default)) {
      return;
    }
  }

  auto start = kratos::util::get_os_time_millionsecond();
  while (true) {
    if (service.is_wait_stop()) {
      break;
    }

    service.update();
    
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
  }

  service.stop();
}

int main(int argc, const char **argv) {
  setup();
  run(argc, argv);
  return 0;
}
